# Maintainer: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=seacas
_releases=2022-01-27
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=${_releases//-/.}
pkgrel=3
pkgdesc="The Sandia Engineering Analysis Code Access System (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
conflicts=("${MINGW_PACKAGE_PREFIX}-libexodus")
depends=("${MINGW_PACKAGE_PREFIX}-crt"
	 "${MINGW_PACKAGE_PREFIX}-metis"
	 "${MINGW_PACKAGE_PREFIX}-matio"
	 "${MINGW_PACKAGE_PREFIX}-netcdf")
makedepends=("unzip"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc")
url="https://gsjaardema.github.io/seacas/"
license=('BSD')
source=("https://github.com/gsjaardema/seacas/archive/refs/tags/v${_releases}.zip"
        "0001-cmake-add-libs.patch")
sha256sums=('D21C14B9B30F773CEF8E2029773F3CC35DA021EEBE9060298231F95021EB814F'
            'a3561756bfdeb942fc62add0aefa542b0e689b76a7f0d2d357e299465ee344d2')
noextract=("v${_releases}.zip") # bsdtar could not unpack this archive

prepare() {  
  rm -rf "${srcdir}/seacas-${_releases}"
  unzip -oq v${_releases}.zip -d "${srcdir}"

  cd "${srcdir}/seacas-${_releases}"
  patch -p1 -i "${srcdir}/0001-cmake-add-libs.patch"
}

build() {
  #Static Build
  [[ -d "${_realname}-${_releases}/build-${MINGW_CHOST}-static" ]] && rm -rf "${_realname}-${_releases}/build-${MINGW_CHOST}-static"
  mkdir -p "${_realname}-${_releases}/build-${MINGW_CHOST}-static" && cd "${_realname}-${_releases}/build-${MINGW_CHOST}-static"
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      INSTALL_PATH=${MINGW_PREFIX} THREADSAFE=ON ../cmake-config \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DSEACASProj_ENABLE_Fortran=OFF \
      -DTPL_ENABLE_X11=OFF \
      -DSEACASProj_ENABLE_TESTS=OFF \
      -DBUILD_SHARED_LIBS=OFF

  ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  [[ -d "../build-${MINGW_CHOST}-shared" ]] && rm -rf "../build-${MINGW_CHOST}-shared"
  mkdir -p "../build-${MINGW_CHOST}-shared" && cd "../build-${MINGW_CHOST}-shared"
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      INSTALL_PATH=${MINGW_PREFIX} THREADSAFE=ON ../cmake-config \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DSEACASProj_ENABLE_Fortran=OFF \
      -DTPL_ENABLE_X11=OFF \
      -DSEACASProj_ENABLE_TESTS=OFF \
      -DBUILD_SHARED_LIBS=YES

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/${_realname}-${_releases}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} cmake --build . --target install
  
  #Shared Install
  cd "${srcdir}/${_realname}-${_releases}/build-${MINGW_CHOST}-shared"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 "${srcdir}/${_realname}-${_releases}"/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  
  #remove useless file, a "fake" mpi interface that allows us to use MPI-enabled codes/libraries (e.g. zoltan) in an application that is built without MPI, conflict with msmpi:
  rm -r ${pkgdir}${MINGW_PREFIX}/include/mpi.h
  #rm -r ${pkgdir}${MINGW_PREFIX}/include/_MPI_UTILITY.h
  #rm -r ${pkgdir}${MINGW_PREFIX}/include/mpi*.h
  #rm -r ${pkgdir}${MINGW_PREFIX}/lib/libsimpi.a 
}
